var address_fun = function()
{
    var province_option = "<option value='0'>省份</option>";
    var city_option = "<option value='0'>城市</option>";
    var district_option = "<option value='0'>县/区</option>";
    var province_rel = $("#province").attr("data");
    var city_rel = $("#city").attr("data");
    var district_rel = $("#district").attr("data");
    var citys_obj = {}, districts_obj = {};
    for (var i = 0; i <= citys.length - 1; i++) {
        if (province_rel != "" && citys[i]['region_id'] == province_rel)
        {
            citys_obj = citys[i]['sub'];
        }
        province_option += "<option value='" + citys[i]['region_id'] + "'>" + citys[i]['region_name'] + "</option>";
    }
    ;
    $("#province").html(province_option);
    sel_fun($("#province"));
    if (citys_obj.length > 0)
    {
        for (var i = 0; i <= citys_obj.length - 1; i++) {
            if (city_rel != "" && citys_obj[i]['region_id'] == city_rel)
            {
                districts_obj = citys_obj[i]['sub'];
            }
            city_option += "<option value='" + citys_obj[i]['region_id'] + "'>" + citys_obj[i]['region_name'] + "</option>";
        }
    }
    $("#city").html(city_option);
    sel_fun($("#city"));
    if (districts_obj.length > 0)
    {
        for (var i = 0; i <= districts_obj.length - 1; i++) {
            district_option += "<option value='" + districts_obj[i]['region_id'] + "'>" + districts_obj[i]['region_name'] + "</option>";
        }
    }
    $("#district").html(district_option);
    sel_fun($("#district"));
}
var provice_fun = function()
{
    var province_val = $("#province").val();
    var citys_obj = {};
    var city_option = "<option value='0'>城市</option>";
    var district_option = "<option value='0'>县/区</option>";
    for (var i = 0; i <= citys.length - 1; i++) {
        if (citys[i]['region_id'] == province_val)
        {
            citys_obj = citys[i]['sub'];
            break;
        }
    }
    if (citys_obj.length > 0)
    {
        for (var i = 0; i <= citys_obj.length - 1; i++) {
            city_option += "<option value='" + citys_obj[i]['region_id'] + "'>" + citys_obj[i]['region_name'] + "</option>";
        }
    }
    $("#city").html(city_option);
    $("#district").html(district_option);
}
var city_fun = function()
{
    var province_val = $("#province").val();
    var city_val = $("#city").val();
    var district_option = "<option value='0'>县/区</option>";
    var citys_obj = {}, district_obj = {};
    for (var i = 0; i <= citys.length - 1; i++) {
        if (citys[i]['region_id'] == province_val)
        {
            citys_obj = citys[i]['sub'];
            break;
        }
    }

    if (citys_obj.length > 0)
    {
        for (var i = 0; i <= citys_obj.length - 1; i++) {
            if (citys_obj[i]['region_id'] == city_val)
            {
                district_obj = citys_obj[i]['sub'];
                break;
            }
        }
    }
    if (district_obj.length > 0)
    {
        for (var i = 0; i <= district_obj.length - 1; i++) {
            district_option += "<option value='" + district_obj[i]['region_id'] + "'>" + district_obj[i]['region_name'] + "</option>";
        }
    }
    $("#district").html(district_option);
}
var sel_fun = function(obj)
{
    var rel = obj.attr("data");
    if (rel && obj.find("option[value='" + rel + "']").length > 0)
    {
        obj.find("option[value='" + rel + "']").get(0).selected = true;
    }
}
$(function() {
    function showError(text) {
        submitable = false;
        $(".error-tip").html(text);
        $(".error-tip").show();
        setTimeout(function() {
            $(".error-tip").hide();
        }, 800);

    }
    //信息提示框
    function showTip(text, callback) {
        $('.loadingTip').css('display', 'none');
        $('.msgTip .cont p').html(text);
        $('.msgTip').css('display', 'table');
        if (typeof callback === "function") {
            $(".msgTip").one("closed", function() {
                callback();
            });
        }
    }
    //提示框
    $(".tip-close").bind("click", function() {
        $(".tip-wrap").css("display", "none");
        // submitable = false;
        $(".msgTip").trigger("closed");
    });
    address_fun();
    $("#province").bind("change", function() {
        provice_fun();
    })
    $("#city").bind("change", function() {
        city_fun();
    })


})